import re

import scrapy
from spidertools.utils.time_utils import get_current_date

from commonresources.spider_items.zhejiang.items import ZheJiangShengShuiLiTingItem
from commonresources.spiders.basespider import BaseSpider
from scrapy.http import Request, FormRequest
import time


class ZheJiangShengShuiLiTingSpider(BaseSpider):
    """
        浙江省水利厅  http://slt.zj.gov.cn/
    """
    name = 'ZheJiangShengShuiLiTing'
    name_zh = '浙江省水利厅'
    province = "浙江"

    def __init__(self, full_dose=False):
        self.capacity = 15
        super(ZheJiangShengShuiLiTingSpider, self).__init__(full_dose)

    def parse(self, response):
        pass

    def fake_data(self, columnid):
        return {
            "col": "1",
            "appid": "1",
            "webid": "3028",
            "path": "/",
            "columnid": f"{columnid}",
            "sourceContentType": "1",
            "unitid": "6106185",
            "webname": "浙江省水利厅",
            "permissiontype": "0",
        }

    def start_requests(self):
        classify = [
            ["招标公告", '1229187626'],
            ["招标结果", '1229187628'],
        ]
        for obj in classify:
            announcement_type = obj[0]
            fake_param = obj[-1]
            data = self.fake_data(fake_param)
            url = f"http://slt.zj.gov.cn/module/jpage/dataproxy.jsp?startrecord=1&endrecord=45&perpage={self.capacity}"
            yield scrapy.FormRequest(
                url=url,
                dont_filter=True,
                formdata=data,
                callback=self.handle_response,
                meta={
                    'announcement_type': announcement_type,
                    'page_count': -1,
                    'fake_param': fake_param,
                    'need_break': False,
                    'endrecord': 45,
                }
            )

    def handle_response(self, response):
        if response.meta['page_count'] == -1:
            response.meta['page_count'] = int(re.findall(r'<totalrecord>(\d*?)</totalrecord>', response.text)[0])
        list_objs = re.findall(
            r"<record>.*?href=['\"]{1}(.*?)['\"]{1}.*?title=(.*?)>.*?>(\s*?\d{4}-\s*?\d{2}-\s*?\d{2})</span",
            response.text)
        for obj in list_objs:
            item = dict()
            item['release_time'] = "-".join(map(lambda x: x.strip(), obj[-1].split("-")))
            if not self.full_dose and item['release_time'] != get_current_date():
                response.meta['need_break'] = True
            else:
                item['origin_url'] = obj[0]
                item['announcement_title'] = obj[1].strip("'")
                item['announcement_type'] = response.meta['announcement_type']
                item['item'] = ZheJiangShengShuiLiTingItem()
                yield scrapy.Request(
                    url=item['origin_url'],
                    callback=self.parse_item,
                    meta=item,
                    dont_filter=True,
                )

        if not response.meta['need_break']:
            startrecord = response.meta['endrecord']
            page_count = response.meta['page_count']
            if startrecord < page_count:
                endrecord = startrecord + 3 * self.capacity
                if endrecord > page_count:
                    endrecord = page_count
                startrecord += 1
                url = f"http://slt.zj.gov.cn/module/jpage/dataproxy.jsp?startrecord={startrecord}&endrecord={endrecord}&perpage={self.capacity}"
                fake_param = response.meta['fake_param']
                yield FormRequest(
                    url=url,
                    dont_filter=True,
                    formdata=self.fake_data(fake_param),
                    callback=self.handle_response,
                    meta={
                        'announcement_type': response.meta['announcement_type'],
                        'page_count': page_count,
                        'fake_param': fake_param,
                        'need_break': False,
                        'endrecord': endrecord,
                    }
                )
